CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0)
PROJECT(duckOS C CXX ASM)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_SYSROOT ${CMAKE_BINARY_DIR}/root)
set(CMAKE_STAGING_PREFIX ${CMAKE_BINARY_DIR}/root)
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/root)
set(CMAKE_INSTALL_DATAROOTDIR ${CMAKE_BINARY_DIR}/root/res)

set(CMAKE_SKIP_RPATH TRUE)

set(CMAKE_CXX_STANDARD 17)

SET_PROPERTY(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "root")

# Options
SET(ADD_KERNEL_DEBUG_SYMBOLS OFF CACHE BOOL "Add symbols to the kernel -g and -ggdb for debugging")

IF("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
    SET(KERNEL_EXECUTABLE_NAME duckk32)
ELSEIF("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
    SET(KERNEL_EXECUTABLE_NAME duckk64)
ENDIF()

function(MAKE_LIBRARY LIBNAME)
    # Install dynamic library
    ADD_LIBRARY(${LIBNAME} SHARED ${SOURCES})
    INSTALL(TARGETS ${LIBNAME} LIBRARY DESTINATION lib)
    SET_TARGET_PROPERTIES(${LIBNAME} PROPERTIES PREFIX "")
    ADD_DEPENDENCIES(${LIBNAME} libc)

    # Install static library
    ADD_LIBRARY(${LIBNAME}_static STATIC ${SOURCES})
    INSTALL(TARGETS ${LIBNAME}_static DESTINATION lib)
    SET_TARGET_PROPERTIES(${LIBNAME}_static PROPERTIES PREFIX "")
    ADD_DEPENDENCIES(${LIBNAME}_static libc)

    # Install headers
    FILE(GLOB_RECURSE LIBHEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.h")
    foreach(HEADER ${LIBHEADERS})
        GET_FILENAME_COMPONENT(SUBDIRECTORY ${HEADER} DIRECTORY)
        install(FILES ${HEADER} DESTINATION usr/include/${LIBNAME}/${SUBDIRECTORY})
    endforeach()
    FILE(GLOB_RECURSE LIBHEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.hpp")
    foreach(HEADER ${LIBHEADERS})
        GET_FILENAME_COMPONENT(SUBDIRECTORY ${HEADER} DIRECTORY)
        install(FILES ${HEADER} DESTINATION usr/include/${LIBNAME}/${SUBDIRECTORY})
    endforeach()
endfunction()

function(MAKE_PROGRAM PROGNAME)
    ADD_EXECUTABLE(${PROGNAME} ${SOURCES})
    INSTALL(TARGETS ${PROGNAME} RUNTIME DESTINATION bin)
    ADD_DEPENDENCIES(${PROGNAME} libc)
endfunction()

function(MAKE_APP APPNAME)
    ADD_EXECUTABLE(${APPNAME} ${SOURCES})
    INSTALL(TARGETS ${APPNAME} RUNTIME DESTINATION apps/${APPNAME}.app)
    INSTALL(DIRECTORY resources/ DESTINATION apps/${APPNAME}.app)
    ADD_DEPENDENCIES(${APPNAME} libc)
endfunction()

INCLUDE_DIRECTORIES(libraries/)
INCLUDE_DIRECTORIES(libraries/libc)
INCLUDE_DIRECTORIES(.)
include_directories(${TOOLCHAIN_ROOT_DIR}/${TOOLCHAIN_PLATFORM}/include/c++/${GCC_VER}/)
include_directories(${TOOLCHAIN_ROOT_DIR}/${TOOLCHAIN_PLATFORM}/include/c++/${GCC_VER}/${TOOLCHAIN_PLATFORM}/)

LINK_DIRECTORIES(${TOOLCHAIN_ROOT_DIR}/lib/gcc/${TOOLCHAIN_PLATFORM}/${GCC_VER}/)
LINK_DIRECTORIES(${TOOLCHAIN_ROOT_DIR}/${TOOLCHAIN_PLATFORM}/lib/)

ADD_SUBDIRECTORY(kernel/)
ADD_SUBDIRECTORY(libraries/)
ADD_SUBDIRECTORY(services/)
ADD_SUBDIRECTORY(programs/)

ADD_CUSTOM_TARGET(image
        COMMAND ${CMAKE_COMMAND} -E env "SOURCE_DIR=${CMAKE_SOURCE_DIR}" "ARCH=${CMAKE_SYSTEM_PROCESSOR}" ${CMAKE_SOURCE_DIR}/scripts/image.sh $(IMAGE_DEV)
        BYPRODUCTS ${CMAKE_BINARY_DIR}/duckOS.img
        USES_TERMINAL
)

ADD_CUSTOM_TARGET(base-system
        COMMAND ${CMAKE_COMMAND} -E env "SOURCE_DIR=${CMAKE_SOURCE_DIR}" ${CMAKE_SOURCE_DIR}/scripts/base-system.sh $(ROOT_DIR)
        USES_TERMINAL
)

ADD_CUSTOM_TARGET(qemu
	COMMAND ${CMAKE_COMMAND} -E env "KERNEL_NAME=${KERNEL_EXECUTABLE_NAME}" "ARCH=${CMAKE_SYSTEM_PROCESSOR}" ${CMAKE_SOURCE_DIR}/scripts/qemu.sh
	USES_TERMINAL
)

ADD_CUSTOM_TARGET(tests
	COMMAND ${CMAKE_COMMAND} -E env "KERNEL_NAME=${KERNEL_EXECUTABLE_NAME}" "ARCH=${CMAKE_SYSTEM_PROCESSOR}" ${CMAKE_SOURCE_DIR}/scripts/qemu.sh kernel-tests=true
    USES_TERMINAL
)
